package com.example.paichaapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.paichaapp.R;
import com.example.paichaapp.model.ContactPerson;
import com.tencent.mmkv.MMKV;

import java.util.List;

public class AlarmSettingsAdapter extends ArrayAdapter<ContactPerson> {

    private int resourceId;

    public AlarmSettingsAdapter(Context context, int textViewResourceId, List<ContactPerson> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ContactPerson contactPerson = getItem(position);
        MMKV mmkv = MMKV.mmkvWithID("contactPerson");
        View view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
        TextView personName = (TextView) view.findViewById(R.id.alarm_settings_person_name);
        personName.setText(contactPerson.getPersonName());
        Switch messageSwitch = (Switch) view.findViewById(R.id.message_alarm);
        if (contactPerson.getSwitchSet().equals("on")){
            messageSwitch.setChecked(true);
        } else {messageSwitch.setChecked(false);}
        messageSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    int i=position+1;
                    mmkv.encode("switch"+i,"on");
                }else {
                    int i=position+1;
                    mmkv.encode("switch"+i,"off");
                }
            }
        });
        return view;
    }
}
